package com.mlnx.work.service.iml;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mlnx.common.utils.MyLog;
import com.mlnx.work.dao.mysql.UserMapper;
import com.mlnx.work.enums.ResponseEnum;
import com.mlnx.work.exception.WorkServiceException;
import com.mlnx.work.pojo.vo.UserVo;
import com.mlnx.work.service.UserService;

import org.shan.spring.cache.CacheChannel;
import org.shan.spring.mybatis.service.BaseCurdService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by amanda.shan on 2019/7/15.
 */
public class BaseService<M extends BaseMapper<T>, T> extends BaseCurdService<M, T> {

    protected MyLog log = MyLog.getLog(getClass());

    private static ThreadLocal threadLocal = new ThreadLocal();

    @Autowired
    protected CacheChannel cacheChannel;

    protected static UserService userService;

    protected static UserMapper userMapper;

    protected void evict(String region, String key) {
        cacheChannel.evict(region, key);
    }

    protected void clear(String region) {
        cacheChannel.clear(region);
    }

    public void setUsername(String username) {
        threadLocal.set(username);
    }

    public String getUsername() {

        Object username = threadLocal.get();
        if (username != null && !username.equals("anonymousUser")) {
            return username.toString();
        } else {
            throw new WorkServiceException(ResponseEnum.USER_FAIL.getCode(), "用户未登入");
        }
    }

    public UserVo getSelfUser() {

        return userMapper.selectUserVo(getUsername());
    }

    public Integer getSelfUserId() {
        return getSelfUser().getId();
    }

    public Integer getSelfDeptId() {
        return getSelfUser().getDeptId();
    }
}
